home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / bin / ginsu / ginsu.C < prev    next >
C/C++ Source or Header  |  1993-11-17  |  9KB  |  319 lines

  1. #define poly ___poly
  2. extern "C"
  3. {
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/time.h>
  9. #include "forms.h"
  10. #include "ginsuF.h"
  11. #include "ooglwrap.h"
  12.  
  13. int sginap(long);
  14. }
  15. #undef poly
  16.  
  17. #include "clip.h"
  18.  
  19. int resworld=0;
  20.  
  21. void set_transform(float M[4][4], float a, float b, float c, float d)
  22. {
  23.   float tmp, sc, sc2;
  24.   float xa, ya, za;
  25.   float xb, yb, zb;
  26.   
  27.  
  28.   tmp = (float)sqrt((double)(a*a + b*b + c*c));
  29.   sc = sqrt(tmp)/2.0;
  30.   sc2 = 1.0/(tmp*tmp);
  31.   M[0][0] = a/tmp; M[1][0] = b/tmp; M[2][0] = c/tmp; M[3][0] = 0.0;
  32.  
  33.   xa = M[0][0]; ya = M[1][0]; za = M[2][0];
  34.   xb = M[0][0]; yb = M[1][0]; zb = M[2][0];
  35.  
  36.   if (xa>-.00001 && xa<.00001)
  37.     xb += 10.0;
  38.   else
  39.     yb += 10.0;
  40.  
  41.   tmp = xa*xb + ya*yb + za*zb;
  42.   xb -= tmp*xa; yb -= tmp*ya; zb -= tmp*za;
  43.   tmp = (float)sqrt((double)(xb*xb + yb*yb + zb*zb));
  44.   xb = xb/tmp; yb = yb/tmp; zb = zb/tmp;
  45.   M[0][1] = xb; M[1][1] = yb; M[2][1] = zb; M[3][1] = 0.0;
  46.  
  47.   M[0][2] = ya*zb- za*yb;
  48.   M[1][2] = za*xb - xa*zb;
  49.   M[2][2] = xa*yb - ya*xb;
  50.   tmp = (float)sqrt((double)(M[0][2]*M[0][2] + M[1][2]*M[1][2]
  51.         + M[2][2]*M[2][2]));
  52.   M[0][2] = M[0][2]/tmp;
  53.   M[1][2] = M[1][2]/tmp;
  54.   M[2][2] = M[2][2]/tmp;
  55.  
  56.   M[0][3] = d*a; M[1][3] = d*b; M[2][3] = d*c; M[3][3] = 1.0;
  57.  
  58.   fprintf(stdout,"(xform-set clip_plane { ");
  59.   fprintf(stdout,"%f %f %f %f ",M[0][1]*sc,M[1][1]*sc,M[2][1]*sc,M[3][1]);
  60.   fprintf(stdout,"%f %f %f %f ",M[0][2]*sc,M[1][2]*sc,M[2][2]*sc,M[3][2]);
  61.   fprintf(stdout,"%f %f %f %f ",M[0][0]*sc,M[1][0]*sc,M[2][0]*sc,M[3][0]);
  62.   fprintf(stdout,"%f %f %f %f })\n",M[0][3]*sc2,M[1][3]*sc2,
  63.             M[2][3]*sc2,M[3][3]);
  64.   fflush(stdout);
  65. }
  66.  
  67. void update_transform(int skip)
  68. {
  69.   char str[100];
  70.   float T[4][4];
  71.   float x[3],y[3],z[3];
  72.   float av,bv,cv,dv;
  73.   static struct timeval notime = {0, 0};
  74.   static struct timeval timeout = {0, 200000};
  75.   FILE *in = stdin;
  76.   char c = ' ';
  77.   int   count=0;
  78.  
  79.   c = ' ';
  80.   if (skip)
  81.   {
  82.    fprintf(stdout,"( write transform - clip_plane wrap )\n");
  83.    fprintf(stdout,"( echo q )\n");
  84.    fflush(stdout);
  85.   }
  86.   while ((c!='q')&&(!feof(stdin)))
  87.   {
  88.    while ((c!='\n')&&(c!='q')&&(!feof(stdin)))
  89.    {
  90.     c = (char) fgetc(stdin);
  91.     /*fputc((int)c,stderr);
  92.     fputc((int)':',stderr);
  93.     fprintf(stderr,"%d",(int)c);*/
  94.    }
  95.    if ((c=='q')||(feof(stdin))) exit(0);
  96.    fscanf(stdin," %f %f %f %f\n",&T[0][0],&T[1][0],&T[2][0],&T[3][0]);
  97.    fscanf(stdin," %f %f %f %f\n",&T[0][1],&T[1][1],&T[2][1],&T[3][1]);
  98.    fscanf(stdin," %f %f %f %f\n",&T[0][2],&T[1][2],&T[2][2],&T[3][2]);
  99.    fscanf(stdin," %f %f %f %f\n",&T[0][3],&T[1][3],&T[2][3],&T[3][3]);
  100.    count=0;
  101.    while ((c!='q')&&(!feof(stdin)))
  102.    {
  103.     c = (char) fgetc(stdin);
  104.     /*fputc((int)c,stderr);
  105.     fputc((int)'!',stderr);*/
  106.    }
  107.   if (c!='q') exit(0);
  108.   }
  109.   x[0] = T[0][0]*1.0+T[0][1]*1.0+T[0][2]*0.0+T[0][3]*1.0;
  110.   y[0] = T[1][0]*1.0+T[1][1]*1.0+T[1][2]*0.0+T[1][3]*1.0;
  111.   z[0] = T[2][0]*1.0+T[2][1]*1.0+T[2][2]*0.0+T[2][3]*1.0;
  112.   x[1] = T[0][0]*1.0+T[0][1]*(-1.0)+T[0][2]*0.0+T[0][3]*1.0;
  113.   y[1] = T[1][0]*1.0+T[1][1]*(-1.0)+T[1][2]*0.0+T[1][3]*1.0;
  114.   z[1] = T[2][0]*1.0+T[2][1]*(-1.0)+T[2][2]*0.0+T[2][3]*1.0;
  115.   x[2] = T[0][0]*(-1.0)+T[0][1]*(-1.0)+T[0][2]*0.0+T[0][3]*1.0;
  116.   y[2] = T[1][0]*(-1.0)+T[1][1]*(-1.0)+T[1][2]*0.0+T[1][3]*1.0;
  117.   z[2] = T[2][0]*(-1.0)+T[2][1]*(-1.0)+T[2][2]*0.0+T[2][3]*1.0;
  118.  
  119.   fprintf(stdout,"( write transform - clip_plane wrap )\n");
  120.   fflush(stdout);
  121.   
  122.   resworld++;
  123.   if (resworld>4)
  124.   {
  125.    resworld=0;
  126.    fprintf(stdout,"( normalization World keep )\n");
  127.   }
  128.   fprintf(stdout,"( echo q )\n");
  129.   fflush(stdout);
  130.   av = (y[1]-y[0])*(z[2]-z[0])-(z[1]-z[0])*(y[2]-y[0]);
  131.   bv = (z[1]-z[0])*(x[2]-x[0])-(x[1]-x[0])*(z[2]-z[0]);
  132.   cv = (x[1]-x[0])*(y[2]-y[0])-(y[1]-y[0])*(x[2]-x[0]);
  133.   dv = av*x[0]+bv*y[0]+cv*z[0];
  134.   sprintf(str, "    %7.5f",-av);
  135.   fl_set_input(Ainput,str);
  136.   sprintf(str, "    %7.5f",-bv);
  137.   fl_set_input(Binput,str);
  138.   sprintf(str, "    %7.5f",-cv);
  139.   fl_set_input(Cinput,str);
  140.   sprintf(str, "    %7.5f",-dv);
  141.   fl_set_input(Dinput,str);
  142. }
  143.  
  144. void set_info(FL_OBJECT *myinfo)
  145. {
  146.  int i;
  147.  static char *inf[] = {
  148.  "By Daeron Meyer",
  149.  "Copyright (c) 1992",
  150.  "The Geometry Center",
  151.  "anonymous ftp: geom.umn.edu",
  152.  "email: software@geom.umn.edu",
  153.  " ",
  154.  "Ginsu is free software and is",
  155.  "designed to be used as a tool",
  156.  "for Geomview. It allows the user",
  157.  "to interactively cut an object",
  158.  "within Geomview and then view",
  159.  "the resulting pieces."
  160.  };
  161.  for (i=0;i<12;i++)
  162.   fl_add_browser_line(myinfo, inf[i]);
  163. }
  164.  
  165. main()
  166. {
  167.  FL_OBJECT *retobj = NULL;
  168.  char c = ' ';
  169.  char *targ, str[100];
  170.  int  update_count=0;
  171.  int  count, xc, yc;
  172.  int  editon = 0;
  173.  float ap,bp,cp,dp;
  174.  float M[4][4];
  175.  char *pass[8];
  176.  
  177.  for (count=0;count<8;count++)
  178.   pass[count] = new char[100];
  179.  for (yc=0; yc<4; yc++)
  180.   for (xc=0; xc<4; xc++)
  181.   {
  182.     if (yc==xc)
  183.       M[xc][yc] = 1;
  184.     else
  185.       M[xc][yc] = 0;
  186.   }
  187.  foreground();
  188.  fl_init();
  189.  create_the_forms();
  190.  set_info(MyBrowser);
  191.  fl_show_form(ClipPanel, FL_PLACE_SIZE, TRUE, "Ginsu");
  192.  fprintf(stdout,"( geometry clip_plane {\nCQUAD 1 1 0 1 0 0 1 -1 1 0 1 0 0 1 -1 -1 0 0.2 0.2 0.5 1 1 -1 0 0.2 0.2 0.5 1})\n");
  193.  /*fprintf(stdout,"( geometry clip_plane { QUAD 1 1 0 -1 1 0\
  194.   \ -1 -1 0 1 -1 0 })\n");*/
  195.  fprintf(stdout,"( write transform - clip_plane wrap )\n");
  196.  fprintf(stdout,"( echo q )\n");
  197.  fflush(stdout);
  198.  
  199.  while (retobj!=ExitButton)
  200.  {
  201.   if (editon)
  202.     retobj = fl_do_forms();
  203.   else
  204.     retobj = fl_check_forms();
  205.   if (retobj==ExitButton)
  206.   {
  207.      fprintf(stdout,"( delete clip_plane )");
  208.      fflush(stdout);
  209.      exit(0);
  210.   }
  211.   else
  212.   if (retobj==InfoButton)
  213.   {
  214.    fl_show_form(InfoPanel, FL_PLACE_SIZE, TRUE, "Info");
  215.   }
  216.   else
  217.   if (retobj==CloseButton)
  218.   {
  219.    fl_hide_form(InfoPanel);
  220.   }
  221.   else
  222.   if (retobj==EditButton)
  223.   {
  224.    editon = fl_get_button(EditButton);
  225.    fl_freeze_form(ClipPanel);
  226.    update_transform(0);
  227.    update_transform(0);
  228.    fl_unfreeze_form(ClipPanel);
  229.    update_count = 0;
  230.   }
  231.   else
  232.   if (retobj == Ainput || retobj == Binput
  233.     || retobj == Cinput || retobj == Dinput)
  234.   {
  235.     targ = fl_get_input(Ainput);
  236.     sscanf(targ,"%f",&ap);
  237.     sprintf(str, "    %7.5f",ap);
  238.     fl_set_input(Ainput,str);
  239.     targ = fl_get_input(Binput);
  240.     sscanf(targ,"%f",&bp);
  241.     sprintf(str, "    %7.5f",bp);
  242.     fl_set_input(Binput,str);
  243.     targ = fl_get_input(Cinput);
  244.     sscanf(targ,"%f",&cp);
  245.     sprintf(str, "    %7.5f",cp);
  246.     fl_set_input(Cinput,str);
  247.     targ = fl_get_input(Dinput);
  248.     sscanf(targ,"%f",&dp);
  249.     sprintf(str, "    %7.5f",dp);
  250.     fl_set_input(Dinput,str);
  251.     if (!((ap==0.0)&&(bp==0.0)&&(cp==0.0)))
  252.       set_transform(M, ap, bp, cp, dp);
  253.   }
  254.   else
  255.   if (retobj==SliceButton)
  256.   {
  257.    system("rm -f /tmp/ginsu*");
  258.    targ=fl_get_input(TargInput);
  259.    if (strlen(targ)&&strcmp(targ,"clip_plane"))
  260.    {
  261.     if ((!strcmp(targ,"World"))||(!strcmp(targ,"g0"))||(!strcmp(targ,"world")))
  262.     {
  263.      fprintf(stdout,"( delete clip_plane )\n");
  264.      fprintf(stdout,"( write geometry \"/tmp/ginsu.in\" %s bare )\n",targ);
  265.  
  266.      fflush(stdout);
  267.     }
  268.     else
  269.      fprintf(stdout,"( write geometry \"/tmp/ginsu.in\" %s wrap )\n",targ);
  270.  
  271.     fprintf(stdout,"( echo x )\n");
  272.     fflush(stdout);
  273.     c = ' ';
  274.     while ((c!='x')&&(!feof(stdin)))
  275.     c = (char) fgetc(stdin);
  276.     if (c!='x') exit(0);
  277.     targ = fl_get_input(Ainput);
  278.     sscanf(targ,"%f",&ap);
  279.     targ = fl_get_input(Binput);
  280.     sscanf(targ,"%f",&bp);
  281.     targ = fl_get_input(Cinput);
  282.     sscanf(targ,"%f",&cp);
  283.     targ = fl_get_input(Dinput);
  284.     sscanf(targ,"%f",&dp);
  285.     CCWrap("/tmp/ginsu.in","/tmp/ginsu.out");
  286.     sprintf(pass[0],"clip");
  287.     sprintf(pass[1],"/tmp/ginsu.out");
  288.     sprintf(pass[2],"/tmp/ginsu.l");
  289.     sprintf(pass[3],"-l");
  290.     sprintf(pass[4],"%f",ap);
  291.     sprintf(pass[5],"%f",bp);
  292.     sprintf(pass[6],"%f",cp);
  293.     sprintf(pass[7],"%f",dp);
  294.     main_clip(8,pass);
  295.     sprintf(pass[2],"/tmp/ginsu.g");
  296.     sprintf(pass[3],"-g");
  297.     main_clip(8,pass);
  298.     targ=fl_get_input(TargInput);
  299.     fprintf(stdout,"(progn ( delete %s )\n",targ);
  300.     fprintf(stdout,"( new-geometry ginsu.l < \"/tmp/ginsu.l\" )");
  301.     fprintf(stdout,"( new-geometry ginsu.g < \"/tmp/ginsu.g\" ))\n");
  302.     /*fprintf(stdout,"( read transform { define T < \"/tmp/clip_plane\" } )\n");*/
  303.  fprintf(stdout,"( geometry clip_plane {\nCQUAD 1 1 0 1 0 0 1 -1 1 0 1 0 0 1 -1 -1 0 0.2 0.2 0.5 1 1 -1 0 0.2 0.2 0.5 1})\n");
  304.  /*fprintf(stdout,"( geometry clip_plane { QUAD 1 1 0 -1 1 0\
  305.   \ -1 -1 0 1 -1 0 })\n");*/
  306.     fflush(stdout);
  307.     update_transform(1);
  308.    }
  309.   }
  310.   update_count++;
  311.   sginap((long)1);
  312.   if (update_count>50 && !editon)
  313.   {
  314.    update_transform(0);
  315.    update_count=0;
  316.   }
  317.  }
  318. }
  319.